package xju_3_4.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import xju_3_4.interceptor.HelloInterceptor;
import xju_3_4.interceptor.MyWebRequestInterceptor;

@Configuration
@ComponentScan(basePackages = "xju_3_4")
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {

    // 配置视图解析器
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/pages/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    // 配置拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HelloInterceptor())
                .addPathPatterns("/**") // 拦截路径
                .excludePathPatterns("/interceptor"); // 排出拦截路径
        registry.addWebRequestInterceptor(new MyWebRequestInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/interceptor");
    }
}
